package me.yangzhi.day_15;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import me.yangzhi.day_15.model.Movie;

public class MovieAdapter extends ArrayAdapter<Movie> {

    private Context mContext;
    private int mResource;

    public MovieAdapter(@NonNull Context context, int resource, @NonNull List<Movie> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Get the movie object for the current position
        Movie movie = getItem(position);

        // Inflate the layout for each list item
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);

        // Get the TextViews from the layout
        TextView textViewTitle = convertView.findViewById(R.id.textViewTitle);
        TextView textViewDirector = convertView.findViewById(R.id.textViewDirector);
        TextView textViewYear = convertView.findViewById(R.id.textViewYear);
        TextView textViewRating = convertView.findViewById(R.id.textViewRating);

        // Set the movie data to the TextViews
        if (movie != null) {
            textViewTitle.setText(movie.getTitle());
            textViewDirector.setText("导演: " + movie.getDirector());
            textViewYear.setText("年份: " + movie.getYear());
            textViewRating.setText("评分: " + movie.getRating());
        }

        return convertView;
    }
} 